    
    function setVolume(vol){
        var player = document.getElementById('player');
        player.volume = vol/10;
    }
    
    (function(jQuery){
        
        $(window).on('ready',function($){
            getCurrentSong(true);
            addControlsBehavior();
            initShowIndicator();
            show_indicator = setInterval(function(){
                initShowIndicator();
            },5000);
        });
        
        
        var listeners                  = false;
        var nth_song_request           = 2;
        var latest_song                = false;
        var periodical_current_song    = false;
        var request_current_song       = false;
        var periodical_cycle           = 0;
        var show_indicator             = false;
        
        function getCurrentSong(force){
            
            if($.type(periodical_current_song)=='object')
                request_current_song.abort();
            
            getCurrentSongRequest(force);
             
            if($.type(periodical_current_song)=='number')
                window.clearInterval(periodical_current_song);
            
            periodical_current_song = setInterval(function(){
                getCurrentSongRequest();
            },10000);

        }
        
        function getCurrentSongRequest(force){
            var args = '';
            if(force==true)
                args = '/force';
            
            request_current_song = $.get(HOME_URL+'/shoutcast').done(function(data){
                if(data){
                    if(periodical_cycle%nth_song_request==0){ // jeden zweiten request
                       getListener();
                    }
                    var json = $.parseJSON(data);
                    if(json.song!=latest_song){
                        $('#currentsong').html(json.song + ' <span class="show">' + json.show.name + '</span>');
                        if(json.podcast.name){
                            switch(json.show.podcast.every_nth_song){
                                default:
                                    var numtag = 'th';
                                    break;
                                case 1:
                                    var numtag = 'st';
                                    break;
                                
                                case 2:
                                    var numtag = 'nd';
                                    break;
                                    
                                case 3:
                                    var numtag = 'rd';
                                    break;
                            }
                            if(json.show.podcast.every_nth_song>0){
                                $('#currentsong').append('<span class="podcast"> With Podcast: <strong>'+json.podcast.name+'</strong> every <strong>'+json.show.podcast.every_nth_song+''+numtag+'</strong> track</span');
                            }
                        }
                        latest_song = json.song;
                        drawSongHistory();
                    }
                    periodical_cycle++;
                }
            });
        }
        
        
        
        function addControlsBehavior(){
            
            $('.play-button').on('click',function(){
                var player = $('#player')[0];
                player.src=STREAM_URL;
                player.play();
                this.blur();
                $('.stop-button').show();
                $(this).hide();
                $('.might').addClass('on');
                getListener();
            });
            
            $('.stop-button').on('click',function(){
                var player = $('#player')[0];
                player.pause();
                player.src='';
                this.blur();
                $('.play-button').show();
                $(this).hide();
                $('.might').removeClass('on');
                getListener();
                $('.listener i.me').remove();
            });
            
            $('.volume-control').slideControl({
                lowerBound : 0,
                upperBound : 10
            });
            
            $('.volume-control').on('change',function(){
                //console.log($(this).val());
            });
            
            $('.button-toggle-timetable').on('click', function(){
                $('.timetable').toggle(); 
            });
        }
        
        function drawSongHistory(){
            $.get(HOME_URL+'/songhistory').done(function(data){
                if(data){
                    var song_history = $.parseJSON(data);
                    $('.history').html('');
                    for(var i = 1; i < song_history.length; i++){
                        $('.history').append('<div class="song row'+i+'">'+song_history[i].TITLE+'</div>');
                    }
                }
            });
        }
        
        function getListener(){
            setTimeout(function(){
                $.get(HOME_URL+'/listener').done(function(data){
                    if(data){
                        $('.listener').html(data);
                    }
                }); 
            },1000);
            
            
        }
        
        function initShowIndicator(){
            var d               = new Date();
            var seconds         = new Date(d.getFullYear(),d.getMonth(),d.getDate(),0,0,0,0);
            var now_seconds     = (d.getTime() - seconds)/1000;
            var percent         = (100/ALL_OVER_DURATION)*(now_seconds+YESTERDAY_DURATION);
            
            if(show_indicator!=false){
                $('.show-indicator').css({left:percent+'%'});
            } else {
                $('.show-indicator').animate({left:percent+'%'},{duration:2000});
            }
        }
        
        
        
        
    })();